package com.scs.application.modules.finance.controller;

import com.alibaba.fastjson.JSONArray;
import com.scs.application.core.consts.ErrorCode;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.mvc.controller.FlowController;
import com.scs.application.modules.finance.entity.AccountCheck;
import com.scs.application.modules.finance.service.AccountCheckService;
import com.scs.application.modules.sys.model.RestResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
* @Description：扎账单
*/
@RestController
@RequestMapping("/finance/account_check")
public class AccountCheckController extends FlowController<AccountCheckService, AccountCheck> {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * @Description：扎账操作，即初始化扎账单数据
     */
    @PostMapping("/checkInit")
    public RestResult checkInit(@RequestBody Map<String, String> reqData)  {
        RestResult result=RestResult.ok();
        try {
            result=service.checkInit(reqData);
            if(result.getCode() != 0) throw new BusinessException(result.getMsg());
        }catch (Exception e){
            logger.error(e.getMessage());
            logger.error("checkInit",e);
            result.setCode(ErrorCode.COMMON_ERROR);
            result.setMsg("操作失败,"+e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * @Description：业务单号扫描扎账
     */
    @GetMapping("/scanBusKey")
    public RestResult scanBusKey(String busKey)  {
        RestResult result=RestResult.ok();
        try {

           service.scanBusKey(busKey);
        }catch (Exception e){
            logger.error(e.getMessage());
            logger.error("scanBusKey",e);
            result.setCode(ErrorCode.COMMON_ERROR);
            result.setMsg("操作失败,"+e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }


    /**
     * @Description：批量审核
     */
    @PostMapping("/auditBatch")
    public RestResult auditBatch(@RequestBody JSONArray idArr)  {
        RestResult result=RestResult.ok();
        try {
            if(idArr==null || idArr.size() <1) throw new BusinessException("缺少参数【idArr】");
            service.auditBatch(idArr);
            result.setMsg("操作成功，审核通过数量:"+idArr.size());
        }catch (Exception e){
            logger.error(e.getMessage());
            logger.error("auditBatch",e);
            result.setCode(ErrorCode.COMMON_ERROR);
            result.setMsg("操作失败,"+e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * @Description：查询扎账单唯一部门列表
     */
    @GetMapping("/getDeptsUnique")
    public RestResult getDeptsUnique()  {
        RestResult result=RestResult.ok();
        try {
            result.setData(service.getDeptsUnique());
        }catch (Exception e){
            logger.error(e.getMessage());
            logger.error("getDeptsUnique",e);
            result.setCode(ErrorCode.COMMON_ERROR);
            result.setMsg("操作失败,"+e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }


}
